<%@page import="com.alibaba.fastjson.JSONArray"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page import="weaver.general.Util"%>
<%@page import="weaver.conn.RecordSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String tableName = Util.null2String(request.getParameter("tableName"));
RecordSet rs = new RecordSet();
StringBuffer sql = new StringBuffer();
if( "oracle".equalsIgnoreCase(rs.getDBType()) ){
	
}else{
	sql.append("SELECT (CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END) AS tableName,");
	sql.append("col.colorder AS indexCode ,");
	sql.append("col.name AS columnName ,");
	//sql.append("ISNULL(ep.[value] , '') AS columnInfo ,");
	sql.append("t.name AS dataType ,");
	sql.append("col.length AS length ,");
	sql.append("ISNULL(COLUMNPROPERTY(col.id, col.name, 'Scale'), 0) AS doubleLength ,");
	sql.append("(CASE WHEN COLUMNPROPERTY(col.id, col.name, 'IsIdentity') = 1 THEN 'true' ELSE 'false' END) AS td ,");
	sql.append("CASE WHEN EXISTS ( SELECT 1 FROM sysindexes si INNER JOIN sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN sysobjects so ON so.name = si.name AND so.xtype = 'PK' WHERE sc.id = col.id AND sc.colid = col.colid ) THEN 'true' ELSE 'false' END AS isPk,");
	sql.append("CASE WHEN col.isnullable = 1 THEN 'true' ELSE 'false' END AS isN,");
	sql.append("ISNULL(comm.text, '') AS defaultValue ");
	sql.append("FROM syscolumns col ");
	sql.append("LEFT JOIN systypes t ON col.xtype = t.xusertype ");
	sql.append("inner JOIN sysobjects obj ON col.id = obj.id AND obj.xtype = 'U' AND obj.status >= 0 ");
	sql.append("LEFT JOIN syscomments comm ON col.cdefault = comm.id ");
	sql.append("LEFT JOIN sys.extended_properties ep ON col.id = ep.major_id AND col.colid = ep.minor_id AND ep.name = 'MS_Description' ");
	sql.append("LEFT JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id AND epTwo.minor_id = 0 AND epTwo.name = 'MS_Description' ");
	sql.append("WHERE obj.name = '").append(tableName).append("' ");
	sql.append("ORDER BY col.colorder");
}

JSONObject json = new JSONObject();
System.out.println(sql.toString());
if(rs.executeSql(sql.toString())){
	JSONArray arr = new JSONArray();
	while(rs.next()){
		JSONObject data = new JSONObject();
		data.put("columnName",rs.getString("columnName"));
		data.put("dataType",rs.getString("dataType"));
		data.put("length",rs.getString("length"));
		data.put("doubleLength",rs.getString("doubleLength"));
		data.put("isNull",Boolean.parseBoolean(rs.getString("isN")));
		data.put("isPk",Boolean.parseBoolean(rs.getString("isPk")));
		data.put("defaultValue",rs.getString("defaultValue"));
		data.put("isEdit",false);
		arr.add(data);
	}
	if(arr.size()>0){
		json.put("flag",true);
	}else{
		json.put("flag",false);
		json.put("msg","无数据!");
	}
	json.put("data",arr);
}else{
	json.put("flag",false);
	json.put("msg","执行失败!");
}

out.print(json.toJSONString());
%>
